package com.example.productdataserviceprovider.service;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class FileToExcelServiceImpl extends AbstractService implements FileToExcelService {
    @Override
    public File exportToExcel(String dir) {
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            logger.info("dirFile.path={}"+dirFile.getPath());
            return null;
        }
        File[] files = dirFile.listFiles();
        List<String> fileNames = new ArrayList<>();
        for (File file : files) {
            fileNames.add(file.getName());
        }
        File resultFile=exportToExcel(fileNames);
        return resultFile;
    }

    public File exportToExcel(List<String> fileNames) {
        //1.在内存中创建一个excel文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //2.创建工作簿
        HSSFSheet sheet = hssfWorkbook.createSheet();
        //3.创建标题行
        HSSFRow titlerRow = sheet.createRow(0);
        titlerRow.createCell(0).setCellValue("序号");
        titlerRow.createCell(1).setCellValue("名称");
        int rowIndex=1;
        for (String name : fileNames) {
            HSSFRow dataRow = sheet.createRow(rowIndex );
            dataRow.createCell(0).setCellValue(rowIndex);
            dataRow.createCell(1).setCellValue(name);
            rowIndex++;

        }
         //5.创建文件名
        String fileName = UUID.randomUUID().toString()+".xls";
//        String savePath =this.getClass().getResource("/").getFile();// "d:/jd/";
//        String savePath2 =this.getClass().getResource("/").getFile();// "d:/jd/";
        String savePath ="c:/tmp/";
        File savePathDir=new File(savePath);
        if(!savePathDir.exists())savePathDir.mkdir();
        logger.info("savePath==={}"+savePath);
        File file=new File(savePath+fileName);
        logger.info("f.path={}"+file.getPath());

        try {
//            file.createNewFile();
            FileOutputStream outputStream=new FileOutputStream(file);
            hssfWorkbook.write(outputStream);
            hssfWorkbook.close();
            outputStream.close();
            return file;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
        return null;
    }
    private void changeToBase64(String imagePath){
        InputStream in=null;
        byte[] data=null;
        try {
            in=new FileInputStream(imagePath);
            data=new byte[in.available()];
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException ioe){
            ioe.printStackTrace();
        }
        BASE64Encoder encoder=new BASE64Encoder();
        String result=encoder.encode(data);
        logger.info("result={}"+result);
    }

    public static void main(String[] args) {
//        String line="D:\fileUpload";
//        line=line.replaceAll("\\\\",File.separator);
//                System.out.println(line);

        FileToExcelServiceImpl service=new FileToExcelServiceImpl();
        service.changeToBase64("D:/1547537592352.png");
    }
}
